/*
 * Project: DroidAttack
 * Copyright (C) 2009-2010 Jeffrey Rusterholz <dataforger@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 

package com.google.code.droidattack; 

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class GameActivity extends Activity implements OnClickListener {

	// Menu.
	private static final int MENU_NEW_GAME = Menu.FIRST; 
	private static final int MENU_QUIT_GAME = Menu.FIRST + 1; 

	// StarView
	private StarView mStarView;
	private static String ICICLE_KEY = "Galaxy";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.game);
		mStarView = (StarView) findViewById(R.id.StarView);

		mStarView.setTextMoves((TextView) findViewById(R.id.MovesLeftTV));
		mStarView.setPlayerImage((ImageView) findViewById(R.id.Player_image));
		
		if (savedInstanceState == null) {
            // We were just launched -- set up a new game

        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {
            	mStarView.restoreState(map);
            } else {
                //Pause game.
            }
        }
		
        setOnClick(R.id.End_Move_button);         
	}

	private void setOnClick(int id) {
    	View v = findViewById(id);
    	v.setOnClickListener(this);
    }

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.End_Move_button:
			endMove();
			break;
		}		 		
	}
	
	/**
	 * End the player's move.
	 */
	private void endMove() {
		mStarView.setNumberOfMovesLeft(3);		
	}

	public boolean onCreateOptionsMenu(Menu menu) {      
		menu.add(Menu.NONE, MENU_NEW_GAME, 0, R.string.New_Game)
			.setIcon(R.drawable.ic_menu_stop);
		menu.add(Menu.NONE, MENU_QUIT_GAME, 0, R.string.Quit_Game)
			.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
	    return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { 
		case MENU_NEW_GAME: 
			mStarView.newGame();
			break;
		case MENU_QUIT_GAME:
			// Are you sure?
			mStarView = null;
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
    @Override
    public void onSaveInstanceState(Bundle outState) {
        //Store the game state
        outState.putBundle(ICICLE_KEY, mStarView.saveState());
    }	
}
